//
// Created by hrg on 2019/12/5.
//

#ifndef ECDRIVER_ECDRIVER_H
#define ECDRIVER_ECDRIVER_H

#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>

#include "ecdata.h"
#include "config.h"

class Device;
struct EcParam{
    int8_t m_Mode_;
    long m_Period_;
    long m_Offset_;
};

class EcDriver{
public:
    EcDriver(EcData* ecdata = nullptr, int mid = 0);
    ~EcDriver();

    bool Init(EcParam ecParam, std::string xmldir);
    bool Config();

    bool StartRTask();
    bool StopRTask();

    uint16_t GetErrorCode(int index);

    void SetCurstomInterface(boost::function<Target(EcStatus&)> handle);


    /**
     * @Func
     *      GetDeviceStatus
     * @Berif
     *      get curr device status
     * @Input
     *      none
     * @Output
     *      device status
     */ 
    int8_t GetDeviceStatus();

    int8_t GetOneStatus(int index);
    EcType GetSlvType(int index);

    /**
     * @Func
     *      GetSlvNum
     * @Berif
     *      get the number of slave
     * @Input
     *      none
     * @Output
     *      return the number of slave
     */
    int GetServoNum();
    int GetEcIONum();

    int* GetCurrPos();
    int* GetCurrVel();

    int* GetTargetPos();
    int* GetTargetVel();

    /**
     * @Func
     *      SetIOCmd
     * @Berif
     *      设置IO命令
     * @Input
     *      index: IO模块对应的索引号
     *      cmd  : 下发给IO模块的命令
     * @Output
     *      成功，返回true; 失败，返回false
    */    
    bool SetIOCmd(int index, int cmd);
#if 0
    uint32_t GetIOCmd(int index);
#endif
    /**
     * @brief GetIODig
     * @param index
     * @return 返回数字量
     */
    uint32_t GetIODig(int index);

    /**
     * @brief GetIOAna
     * @param index
     * @return 返回模拟量
     */
    uint32_t GetIOAna(int index, int pdo);

    /**
     * @Func
     *      Reset, AllReset
     * @Berif
     *      重置某一个从站或者重置所有的从站
     * @Input
     *      index: 从站的索引值
     * @Output
     *      none
    */
    void Reset(int index);
    void AllReset();

    void ClearDataCache();
    /**
     * @Func
     *      EnableSlaves
     * @Berif
     *      enable index slave
     * @Input
     *      index: the index of slave
     * @Output
     *      success, return true; or, return false
     */
    bool EnableSlaves(int index);

    /**
     * @Func
     *      DisableSlaves
     * @Berif
     *      disable index slave
     * @Input
     *      index: the index of slave
     * @Output
     *      success, return true; or, return false
     */
    bool DisableSlaves(int index);

    /**
     * @Func
     *      IsEnableSlaveCheck
     * @Berif
     *      whether the current slave station is enabled
     * @Input
     *      index: the index of check
     * @Output
     *      success, return true; or, return false
     */
    bool IsEnableSlaveCheck(int index);
private:
    Device *m_Device_;
};

typedef boost::shared_ptr<EcDriver> EcDriverPtr;

#endif //ECDRIVER_ECDRIVER_H
